package retrofit.boot.autoconfigure;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import retrofit.boot.client.RetrofitErrorHandler;
import retrofit.boot.client.RetrofitRequestInterceptor;
import retrofit.boot.converter.FastjsonConverter;
import retrofit.client.Client;

/**
 * Created by wulg on 2019/10/28
 * description:
 */
@ConditionalOnClass({Client.class})
@EnableConfigurationProperties({RetrofitClientProperties.class})
public class RetrofitAutoConfiguration {

    public final RetrofitClientProperties properties;

    public RetrofitAutoConfiguration(RetrofitClientProperties properties){
        this.properties = properties;
    }

    @Bean
    @Primary
    public RetrofitErrorHandler errorHandler(){
        return new RetrofitErrorHandler();
    }

    @Bean
    @Primary
    public RetrofitRequestInterceptor requestInterceptor(){
        return new RetrofitRequestInterceptor();
    }

    @Bean
    @Primary
    public FastjsonConverter converter(){
        return new FastjsonConverter(properties.getDefalutCharset());
    }

    @Bean
    @Primary
    public RetrofitContext retrofitContext(){
        return new RetrofitContext(properties);
    }
}
